import axios from "axios";

import $storage from "@/utils/storage.js";
import { Message } from "element-ui";

// axios 基础URL配置
const baseURL = process.env.VUE_APP_BASE_API;

// axios 请求配置
const config = {
  baseURL: baseURL,
  timeout: 600000, //设置最大请求时间 毫秒
};

// 实例化 axios
const _axios = axios.create(config);

/* 请求拦截器（请求之前的操作） */
_axios.interceptors.request.use(
  (config) => {
    //添加token,需要结合自己的实际情况添加，
    config.headers["authentication"] = $storage.getUserToken() || "";
    // 应用客户端标识
    config.headers["x-header-client"] = "WLTX-ADMIN-API";
    return config;
  },
  (err) => Promise.reject(err)
);

/* 请求之后的操作 */
_axios.interceptors.response.use(
  (res) => {
    return res;
  },
  (err) => {
    return Promise.reject(err);
  }
);

//封装post,get方法,其他的自行往下加，比如put,delete
const http = {
  get(url = "", params = {}) {
    return new Promise((resolve, reject) => {
      _axios({
        url,
        params,
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "GET",
      })
        .then((res) => {
          if (res.status !== 200) {
            Message.error("系统异常");
            throw "系统异常";
          }
          if (res.data.code !== 200) {
            Message.error(res.data.message);
            throw res.data.message;
          } else {
            Message.success(res.data.message);
          }
          resolve(res.data);
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  post(url = "", data = {}, title = true) {
    return new Promise((resolve, reject) => {
      _axios({
        url,
        data: data,
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "POST",
      })
        .then((res) => {
          if (res.status !== 200) {
            Message.error("系统异常");
            throw "系统异常";
          }
          if (res.data.code !== 200) {
            Message.error(res.data.message);
            throw res.data.message;
          } else if (title) {
            Message.success(res.data.message);
          }
          resolve(res.data);
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  delete(url = "", data = {}) {
    return new Promise((resolve, reject) => {
      _axios({
        url,
        data: data,
        headers: { "Content-Type": "application/json;charset=UTF-8" },
        method: "DELETE",
      })
        .then((res) => {
          if (res.status !== 200) {
            Message.error("系统异常");
            throw "系统异常";
          }
          if (res.data.code !== 200) {
            Message.error(res.data.message);
            throw res.data.message;
          } else {
            Message.success(res.data.message);
          }
          resolve(res.data);
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // 上传文件
  upload(url = "", formData = {}, uploadProgress) {
    return new Promise((resolve, reject) => {
      _axios({
        url,
        data: formData,
        headers: { "Content-Type": "multipart/form-data;charset=UTF-8" },
        method: "POST",
        onUploadProgress: (progressEvent) => {
          if (uploadProgress) {
            uploadProgress(progressEvent);
          }
        },
      })
        .then((res) => {
          if (res.status !== 200) {
            Message.error("系统异常");
            throw "系统异常";
          }
          resolve(res.data);
          return res;
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

export default http;
